/**
  ******************************************************************************
  * File Name          : main.c
  * Description        : Main program body
  ******************************************************************************
  ** This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether 
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * COPYRIGHT(c) 2017 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_hal.h"

/* USER CODE BEGIN Includes */
#include "includes.h"
#include "BSP_UART.h"
#include "arm_math.h"
/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim3;

UART_HandleTypeDef huart1;
DMA_HandleTypeDef hdma_usart1_rx;
DMA_HandleTypeDef hdma_usart1_tx;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
Motor_Status MotorStatus;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_TIM1_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);                                    
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
                                
                                

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */



#define CLK_0    HAL_GPIO_WritePin(SPI_CLK_GPIO_Port,SPI_CLK_Pin,GPIO_PIN_RESET)
#define CLK_1    HAL_GPIO_WritePin(SPI_CLK_GPIO_Port,SPI_CLK_Pin,GPIO_PIN_SET)

#define DATA_0    HAL_GPIO_WritePin(SPI_DATA_GPIO_Port,SPI_DATA_Pin,GPIO_PIN_RESET)
#define DATA_1    HAL_GPIO_WritePin(SPI_DATA_GPIO_Port,SPI_DATA_Pin,GPIO_PIN_SET)
#define DATA_IN   HAL_GPIO_ReadPin(SPI_DATA_GPIO_Port,SPI_DATA_Pin)

#define CS_0   HAL_GPIO_WritePin(SPI_CS_GPIO_Port,SPI_CS_Pin,GPIO_PIN_RESET)
#define CS_1   HAL_GPIO_WritePin(SPI_CS_GPIO_Port,SPI_CS_Pin,GPIO_PIN_SET)

#define  DelayTime   1
#define q15_scale 32768.f
typedef enum{
	
	Normal,
	SelfTest

}RunningMode;



RunningMode CurrentMode;
uint16_t angle;
float angle_float;
uint8_t uart_data[30];
typedef enum{
	bridgeOut,
	bridgeIn,
	briggeStop
}bridgeStat;
uint16_t speed1 =400;

void ms_delay(uint32_t time){

		while(time--);
}

void SPI_SEND(uint16_t data){
			
	
			CLK_0;
			for(int i=0;i<16;i++){
					CLK_1;	
							if((data<<i)&0x8000)
							{
								DATA_1;
							}
							else{
							 DATA_0;
							}				
							ms_delay(DelayTime);
					CLK_0;
							ms_delay(DelayTime);					
		  }
			
}

void SPI_READ(uint16_t *data){
		
			CLK_0;
			*data = 0;
			for(int i=0;i<16;i++){		
			CLK_1;		
			ms_delay(DelayTime);
			CLK_0;	
			ms_delay(DelayTime);
				  if(DATA_IN){						
						(*data) = (*data)|((uint16_t)(0x8000>>i));				
					}							
			}
}

#define A_H TIM1->CCR4 
#define A_L TIM1->CCR2
#define B_H TIM1->CCR3
#define B_L TIM1->CCR1
#define C_H TIM3->CCR3
#define C_L TIM3->CCR4

void bridgeControl(char bridge,bridgeStat stat){
		
	__IO uint32_t* CCR_H;
	__IO uint32_t* CCR_L;
		
	switch(bridge){
						
		case 'A':CCR_H = &TIM1->CCR4; CCR_L = &TIM1->CCR2;break;
		case 'B':CCR_H = &TIM1->CCR3; CCR_L = &TIM1->CCR1;break;
		case 'C':CCR_H = &TIM3->CCR3; CCR_L = &TIM3->CCR4;break;
		default:return;
	
	}
	
	switch(stat){
		case bridgeOut:*CCR_H = speed1;*CCR_L = speed1;break; 
		case bridgeIn:*CCR_H = 0;*CCR_L = 0;break; 
		case briggeStop:*CCR_H = 0;*CCR_L = 2000;break; 
	}
	
	
}


uint16_t function_A(int16_t angle){
		
		if(angle>=330)
			angle -=360;
	
			if((angle>=-30&&angle<90)||(angle>330&&angle<=360)){
						
					return speed1*arm_sin_q15(((angle+30)*q15_scale/360))/(q15_scale);
				
			}else if(angle>=90&&angle<210){
					
					return speed1*arm_sin_q15(((angle-30)*q15_scale/360))/(q15_scale);
			}else if(angle>=210&&angle<330){
					
					return 0;
			}
		
}

uint16_t function_B(int16_t angle){
		
//	if(angle<0)
//		angle +=360;
		if(angle>=330)
			angle -=360;
	
			if((angle>=-30&&angle<90)){
						
					return 0;
				
			}else if(angle>=90&&angle<210){
					
					return speed1*arm_sin_q15(((angle+30-120)*q15_scale/360))/(q15_scale);
			}else if(angle>=210&&angle<330){
					
					return speed1*arm_sin_q15(((angle-30-120)*q15_scale/360))/(q15_scale);;
			}
		
}

uint16_t function_C(int16_t angle){
		
		if(angle>=330)
			angle -=360;
	
			if((angle>=-30&&angle<90)){
						
					return speed1*arm_sin_q15(((angle-30+120)*q15_scale/360))/(q15_scale);
				
			}else if(angle>=90&&angle<210){
					
					return 0;
			}else if(angle>=210&&angle<330){
					
					return speed1*arm_sin_q15(((angle+30-240)*q15_scale/360))/(q15_scale);;
			}
		
}
uint16_t testANgle;
void sin_control(){
		
		
			
	testANgle = (int16_t)(angle_float*7)%360;
	
		if(testANgle>=360)
			testANgle = 0;
			
	
	
			uint16_t DutyA = function_A(testANgle);
			uint16_t DutyB = function_B(testANgle);
			uint16_t DutyC = function_C(testANgle);
	
		
	
				
			A_H = DutyA; A_L = DutyA;
	
			B_H = DutyB; B_L = DutyB;
	
	    C_H = DutyC; C_L = DutyC;
				

	//HAL_Delay(10);
			
	

}

void connectBridge(char out,char in){
			
		bridgeControl(out,bridgeOut);
		bridgeControl(in,bridgeIn);
	  bridgeControl(('A'+'B'+'C')-out-in,briggeStop);

}

//float e_angle;
int16_t offset;


void checkPhase(uint8_t dir,uint16_t angle){
					

		if(dir == 1)
		{
		angle = angle%360;		
	
		switch(angle/60){
		
			case 0:connectBridge('B','A');break;
			case 1:connectBridge('C','A');break;
			case 2:connectBridge('C','B');break;
			case 3:connectBridge('A','B');break;
			case 4:connectBridge('A','C');break;
			case 5:connectBridge('B','C');break;
			
		}
	}else{
	
			angle = (angle)%360;
		
	
			switch(angle/60){
		
			case 0:connectBridge('A','B');break;
			case 1:connectBridge('A','C');break;
			case 2:connectBridge('B','C');break;
			case 3:connectBridge('B','A');break;
			case 4:connectBridge('C','A');break;
			case 5:connectBridge('C','B');break;
			
		}
		
	}
}

float last_anlge;
float angle_enc = 0;
int16_t angle_speed ;
void read_encoder(){
		
		CS_0;
	
		SPI_SEND(0x8021);	
		SPI_READ(&angle);
		
		CS_1;
	  angle_float = (angle&32767)*360/32767.f;
		MotorStatus.angle = angle_float;
}


float  initial_angle;

uint16_t phase_change_count = 0;
void RunningSelfTest(){
		
		read_encoder();
		
	  initial_angle = MotorStatus.angle;
		last_anlge = MotorStatus.angle;
		angle_enc = 0;	
		while(angle_enc<=360){
										
					checkPhase(1,phase_change_count*60);
									
					phase_change_count++;
					
			    HAL_Delay(500);
			
					read_encoder();
		
					if(MotorStatus.angle-last_anlge>180){
						
						angle_enc+= (MotorStatus.angle-last_anlge-360);	
						
					}else if(MotorStatus.angle-last_anlge<-180){
					
						angle_enc+= (MotorStatus.angle-last_anlge+360);	
					}
					else{
						angle_enc+= (MotorStatus.angle-last_anlge);	
					}
			
					
			    
					last_anlge = MotorStatus.angle;
			
		}
		
	
		CurrentMode = 3;
}
 uint8_t string[40];
uint32_t lastTransTime;

float test2;
int16_t out;
int16_t input;




/* USER CODE END 0 */

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_TIM1_Init();
  MX_TIM3_Init();
  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */
		
		
//		 input = (uint16_t)(1.234/(2*3.14)*q15_scale);
//		 out =  1000*arm_sin_q15(((30)*q15_scale/360))/q15_scale;
//		
//		test2 = out/q15_scale;
//		
//			HAL_UART_Transmit(&huart1,"func1=[",7,100);
//	 for(int i=0;i<360;i++){
//			
//		 uint16_t t = function_A(i);
//		 uint8_t size = sprintf((char*)string,"%d,",t);
//		 
//		 
//		 HAL_UART_Transmit(&huart1,string,size,100);
//		 
//		 
//		 if(i%10==0)
//			 HAL_UART_Transmit(&huart1,"...\n",4,100);
//		 
//		 HAL_Delay(1);
//	 }
//	 HAL_UART_Transmit(&huart1,"]\n\n",2,100);
//	 
//	 
//	 
//	 		HAL_UART_Transmit(&huart1,"func2=[",7,100);
//	 for(int i=0;i<360;i++){
//			
//		 uint16_t t = function_B(i);
//		 uint8_t size = sprintf((char*)string,"%d,",t);
//		 
//		 
//		 HAL_UART_Transmit(&huart1,string,size,100);
//		 
//		 
//		 if(i%10==0)
//			 HAL_UART_Transmit(&huart1,"...\n",4,100);
//		 
//		 HAL_Delay(1);
//	 }
//	 HAL_UART_Transmit(&huart1,"]\n\n",2,100);
//	 
//	 
//	 
//	 		HAL_UART_Transmit(&huart1,"func3=[",7,100);
//	 for(int i=0;i<360;i++){
//			
//		 uint16_t t = function_C(i);
//		 uint8_t size = sprintf((char*)string,"%d,",t);
//		 
//		 
//		 HAL_UART_Transmit(&huart1,string,size,100);
//		 
//		 
//		 if(i%10==0)
//			 HAL_UART_Transmit(&huart1,"...\n",4,100);
//		 
//		 HAL_Delay(1);
//	 }
//	 HAL_UART_Transmit(&huart1,"]\n\n",2,100);
	 		
	
		
	 HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
	 HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
	 HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
	 HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);
	 
	 HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_3);
	 HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_4);
	 
	 //Junru_UART_Receive_IT(&huart1,uart_data,30);
   
	 lastTransTime = HAL_GetTick();
	 
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
                           
    //uint16_t size = sprintf((char*)string,"Count%d\n",count++);
		
			read_encoder();
			sin_control();		
	
		//HAL_Delay(500);
		
//		if(CurrentMode == Normal){
//		
//		checkPhase(1,MotorStatus.angle*7+offset);		
//		
  							
//		}
//		else if(CurrentMode == SelfTest){
//		
//			RunningSelfTest();
//			
//		}
//		if(HAL_GetTick() - lastTransTime>=500)
//		{
//			
//			lastTransTime = HAL_GetTick();
//		}
	
		
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure the Systick interrupt time 
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick 
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* TIM1 init function */
static void MX_TIM1_Init(void)
{

  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_OC_InitTypeDef sConfigOC;
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 1000;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  sBreakDeadTimeConfig.DeadTime = 0;
  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  HAL_TIM_MspPostInit(&htim1);

}

/* TIM3 init function */
static void MX_TIM3_Init(void)
{

  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_OC_InitTypeDef sConfigOC;

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 0;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 1000;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  HAL_TIM_MspPostInit(&htim3);

}

/* USART1 init function */
static void MX_USART1_UART_Init(void)
{

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_HalfDuplex_Init(&huart1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

/** 
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void) 
{
  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA1_Channel2_3_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);

}

/** Configure pins as 
        * Analog 
        * Input 
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, SPI_CS_Pin|SPI_CLK_Pin|SPI_DATA_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pins : SPI_CS_Pin SPI_CLK_Pin */
  GPIO_InitStruct.Pin = SPI_CS_Pin|SPI_CLK_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : SPI_DATA_Pin */
  GPIO_InitStruct.Pin = SPI_DATA_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(SPI_DATA_GPIO_Port, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void _Error_Handler(char * file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1) 
  {
  }
  /* USER CODE END Error_Handler_Debug */ 
}

#ifdef USE_FULL_ASSERT

/**
   * @brief Reports the name of the source file and the source line number
   * where the assert_param error has occurred.
   * @param file: pointer to the source file name
   * @param line: assert_param error line source number
   * @retval None
   */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */

}

#endif

/**
  * @}
  */ 

/**
  * @}
*/ 

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
